﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cesave.Presentation.Permisos.WinForms.Client.ViewModelBase;
using Cesave.Presentation.Permisos.WinForms.Client.Views.Catalogs.Financiamientos;
using Cesave.Application.Permisos.Settings;
using Cesave.Infrastructure.CrossCutting.IoC;
using System.Globalization;
using Cesave.Presentation.Permisos.WinForms.Client.Resources;
using System.Windows.Forms;

namespace Cesave.Presentation.Permisos.WinForms.Client.ViewModels.Catalogs.Financiamientos
{
	public partial class FinanciamientosViewModel
		: ObservableObject
	{
		#region Private properties

		/// <summary>
		/// 
		/// </summary>
		private FinanciamientosView _view;

		/// <summary>
		/// 
		/// </summary>
		private IPUSSettings _pusSettings;

		#endregion

		#region Constructor

		/// <summary>
		/// 
		/// </summary>
		/// <param name="view"></param>
		public FinanciamientosViewModel(FinanciamientosView view)
		{
			if (view == null)
				throw new ArgumentNullException("view");
			_view = view;
			_pusSettings = IoCFactory.Resolve<IPUSSettings>();
			WireUpEvents();
			InitModels();
		}

		#endregion

		#region Event methods

		/// <summary>
		/// 
		/// </summary>
		private void WireUpEvents()
		{
			PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(FinanciamientosViewModel_PropertyChanged);
			_view.Load += LoadView;
			_view.closeButton.Click += (sender, e) =>
			{
				_view.Close();
			};
			_view.saveButton.Click += SaveFinanciamientos;
			_view.financiamientoBindingNavigatorSaveItem.Click += SaveFinanciamientos;
			_view.bindingNavigatorAddNewItem.Click += AddNewFinanciamiento;
			_view.newButton.Click += AddNewFinanciamiento;
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void LoadView(object sender, EventArgs e)
		{
			_view.Text = string.Format(
				CultureInfo.InvariantCulture,
				WindowTitles.FinanciamientosWindowTitle
			);
		}

		#endregion

		#region Private methods
		
		/// <summary>
		/// 
		/// </summary>
		private void InitModels()
		{
			LoadFinanciamientos();
		}

		/// <summary>
		/// /
		/// </summary>
		private void StartWaiting()
		{
			_view.WaitingBar.StartWaiting();
		}

		/// <summary>
		/// 
		/// </summary>
		private void EndWaiting()
		{
			_view.WaitingBar.EndWaiting();
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="message"></param>
		private void ShowMessage(string message)
		{
			MessageBox.Show(
				_view,
				message,
				WindowTitles.FinanciamientosWindowTitle,
				MessageBoxButtons.OK,
				MessageBoxIcon.Error
			);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="control"></param>
		private void FocusDefaultControl(Control control = null)
		{
			if (control == null)
				control = _view.nombreUltraTextEditor;
			_view.ActiveControl = control;
		}

		#endregion

	}
}
